package com.youedata.solr;


import com.youedata.util.Page;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.Group;
import org.apache.solr.client.solrj.response.GroupCommand;
import org.apache.solr.client.solrj.response.GroupResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.GroupParams;
import org.noggit.JSONUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SolrCloudUtilTest {
	static Logger logger = Logger.getLogger(SolrCloudUtilTest.class);

    public Map<String,Object> map = new HashMap<String,Object>();

    public void addBymap() throws Exception {
		map.put("id", 1);
    	map.put("title", "大头鱼火锅");
		map.put("title_t", "大头鱼火锅");
    	map.put("address", "丰台");
    	map.put("age", 20);
    	map.put("content","新鲜美味，老少皆宜");
    	SolrUtil.addIndexByMap(map, SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
		map.put("id", 2);
		map.put("title", "鲫鱼火锅");
		map.put("title_t", "鲫鱼火锅");
		map.put("address", "海淀");
		map.put("age", 21);
		map.put("content","适合孕妇");
    	SolrUtil.addIndexByMap(map, SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
		map.put("id", 3);
		map.put("title", "鳙鱼火锅");
		map.put("title_t", "鳙鱼火锅");
		map.put("address", "朝阳");
		map.put("age", 22);
		map.put("content","朝阳群众专用");
    	SolrUtil.addIndexByMap(map, SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
		map.put("id", 4);
		map.put("title", "鲢鱼火锅");
		map.put("title_t", "鲢鱼火锅");
		map.put("address", "丰台");
		map.put("age", 22);
		map.put("content","丰台群众专用");
		SolrUtil.addIndexByMap(map, SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
		map.put("id", 5);
		map.put("title", "旮旯鱼火锅");
		map.put("title_t", "旮旯鱼火锅");
		map.put("address", "丰台");
		map.put("age", 22);
		map.put("content","北京市朝阳区东坝乡科怡路18号院");
		SolrUtil.addIndexByMap(map, SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
    }




    public void deleteIndexById(String id) throws Exception {
    	SolrUtil.deleteById(id,SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
    }

    public List<Map<String,Object>> quryMap(Map<String,Object> queryKeys,int pageno,int pagesize,String resultJson, int type) throws Exception {
		Page page = new Page(pageno,pagesize);
		page =  SolrUtil.getMapPage(page, null,new String[]{"a",""},SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
		return page.getDataList();
    }

    public void distinctByGroup(){
		//group去重查询
		SolrQuery query = new SolrQuery();//建立一个新的查询
		query.setQuery("address:丰台");
		// 设置通过facet查询为true，表示查询时使用facet机制
		query.setParam(GroupParams.GROUP,true);
		query.setParam(GroupParams.GROUP_FIELD,"age");
		// 设置每个quality对应的
		query.setParam(GroupParams.GROUP_LIMIT,"1");
		// 设置返回doc文档数据，因只需要数量，故设置为0
		query.setRows(10);
		logger.info("solrQuery:"+query.toQueryString());
		QueryResponse response = null;
		try {
			response = SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION).query(query);
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (response !=null) {
			GroupResponse groupResponse =response.getGroupResponse();
			if(groupResponse !=null) {
				List<GroupCommand> groupList =groupResponse.getValues();
				for(GroupCommand groupCommand : groupList){
					List<Group> groups =groupCommand.getValues();
					for(Group group : groups) {
						SolrDocumentList solrDocuments = group.getResult();
						System.out.println("group by  --"+group.getGroupValue()+"-- find result number ："+solrDocuments.getNumFound());
						System.out.println("------------------------------------------");
						for(SolrDocument doc:solrDocuments){
							System.out.println(" values:"+JSONUtil.toJSON(doc));
						}
						System.out.println("___________________________________________________________");
					}
				}
			}
		}
	}


    public static void main(String[] args) throws Exception {
    	SolrCloudUtilTest st = new SolrCloudUtilTest();
		SolrUtil.deleteAll(SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
//		SolrUtil.optimize(SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
//		SolrUtil.deleteAll(SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
//		//测试添加
    	st.addBymap();

		//st.addByBean();
//
//    	//测试删除
//		st.deleteIndexById("100");
//
		//测试分组去重
//		st.distinctByGroup();


    	//修改跟添加操作其实是一样的

    	//测试查询
//    	Map<String,Object> querykeys = new HashMap<String,Object>();
//    	List<String> ids = new ArrayList<String>();
//    	ids.add("100");
//    	ids.add("103");
//    	querykeys.put("address", "北京");
//    	querykeys.put("id",ids );
//    	querykeys.put("name", "大厦");
//		String queryStr = "出租花园 AND (name:东雍 OR id:101)";
//		SolrQuery query = new SolrQuery();
//		query.setQuery(queryStr);
//		query.setParam("id","100");
//		//query.setQuery("出租花园");
//		//query.set("id","100");
//		logger.info("原始solrQuery:"+queryStr);
//		//queryStr = SolrUtil.analysisSearchText(SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION), queryStr);
//		//query.setQuery(queryStr);
//		logger.info("转换后solrQuery:"+queryStr);
//		//query.setQuery(queryStr);
//		Page page = new Page(1,10);
//		page = SolrUtil.getMapPage(page,query,new String[]{"id","addr","name","price_str"},SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
//		if(page.getDataList() != null){
//			for(Object map : page.getDataList()){
//				map = (Map<String,Object>)map;
//				System.out.println("map:"+ JSON.toJSONString(map));
//			}
//		}

//		System.out.println(officeBuildList);

		//测试查询

		String queryStr = "content:\"东\"";
		SolrQuery query = new SolrQuery();
		query.setQuery(queryStr);
		query.setStart(0);
		query.setRows(15);
		logger.info("原始solrQuery:"+queryStr);
		//queryStr = SolrUtil.analysisSearchText(SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION), queryStr);
		//query.setQuery(queryStr);
		logger.info("转换后solrQuery:"+query.toQueryString());
		//query.setQuery(queryStr);
		Page page = new Page(1,10);
		page = SolrUtil.getMapPage(page,query,new String[]{"id","title","content"},SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION));
		if(page.getDataList() != null){
			for(Object map : page.getDataList()){
				map = (Map<String,Object>)map;
				System.out.println("map:"+ map.toString());
			}
		}

//		String content = "你说的什么啊的呀吗了";
//		System.out.println("你说的什么啊的呀吗了 change to : "+SolrUtil.analysisSearchText(SolrCloudSingleClient.getInstance().getSolrCloudClient(SolrCloudSingleClient.COLLECTION), content));



    }

}
